from __future__ import print_function

# -----------------------------------------------------------------------------
class ProgramSettings(object):
    class UsageError(Exception): pass

    __slots__= ("__help", "__file", "__save_model", "__verbose")

    def __init__(self):
        self.__help = False
        self.__file = ""
        self.__save_model = False
        self.__verbose = True

    def __repr__(self):
        return "-f='{0}' -v={1} -s={2}".format(self.__file, self.__verbose, self.__save_model)

    @property
    def help(self):
        return self.__help
    @help.setter
    def help(self, val):
        self.__help = val

    @property
    def file(self):
        return self.__file
    @file.setter
    def file(self, val):
        self.__file = val

    @property
    def save_model(self):
        return self.__save_model
    @save_model.setter
    def save_model(self, val):
        try:
            self.__save_model = bool(int(val))
        except ValueError:
            pass

    @property
    def verbose(self):
        return self.__verbose
    @verbose.setter
    def verbose(self, val):
        try:
            self.__verbose = bool(int(val))
        except ValueError:
            pass

    def parse(self, argv):
        for i, option in enumerate(argv):
            if i == 0:
                continue
            option = option.strip()
            param_value = option.split("=")
            param = param_value[0]
            value = ""
            if len(param_value) > 1:
                value = param_value[1]

            if param in ("-h", "--help"):
                self.help = value if value else True
            if param in ("-f", "--file"):
                self.file = value
            if param in ("-s", "--save-model"):
                self.save_model = value if value else True
            if param in ("-v", "--verbose"):
                self.verbose = value if value else True

    def usage(self, argv):
        print("\nUsage:\n\t", argv[0], " <file>='filename' [options]\n",
              "\nOptions:\n",
              "\t-f, --file=filename\n",
              "\t-v, --verbose     [{0}]\n".format(self.__verbose),
              "\t-s, --save-model  [{0}]\n".format(self.__save_model),
              sep='')

